package top.xphu.zip;

import top.xphu.zip.entiy.Param;
import top.xphu.zip.enums.HttpMethod;
import top.xphu.zip.enums.ParamType;
import top.xphu.zip.service.DownloadFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.util.Map;

/**
 * 文件下载工具类
 * @author xphu
 * @version 1.0
 * @date 2021/7/1 15:58
 */
public class FileDownloadUtils{
    private static DownloadFile downloadFile = DownloadFile.build();


    /**
     * 下载文件
     * @param fis 要下载的file文件流
     * @param fileName 文件名称（不传值则使用默认的名称）
     * @param response  javax.servlet.http.HttpServletResponse
     * @return response javax.servlet.http.HttpServletResponse;
     * @throws Exception
     */
    public static HttpServletResponse download(InputStream fis, String fileName, HttpServletResponse response) throws Exception{
        return downloadFile.download(fis, fileName, response);
    }


    /**
     * 下载文件
     * @param file 要下载的file文件
     * @param fileName 文件名称（不传值则使用默认的名称）
     * @param response  javax.servlet.http.HttpServletResponse
     * @return response javax.servlet.http.HttpServletResponse;
     * @throws Exception
     */
    public static HttpServletResponse download(File file,String fileName, HttpServletResponse response) throws Exception {
        return downloadFile.download(file, fileName, response);
    }

    /**
     *
     * @param url 下载地址
     * @param method 请求方式
     * @return HttpServletResponse
     */
    public static HttpServletResponse httpDownload(String url, HttpMethod method, String fileName, Map<String, String> param, ParamType paramType, HttpServletResponse response) throws Exception{
        return downloadFile.httpDownload(url, method, fileName, param, paramType, response);
    }

    /**
     * 通过get http请求下载文件
     * @param url 下载地址
     * @param param 参数
     * @return HttpServletResponse
     */
    public static HttpServletResponse GetHttpDownload(String url, String fileName, Map<String, String> param, HttpServletResponse response) throws Exception{
        return downloadFile.doGetHttpDownload(url, fileName, param, response);
    }

    /**
     * 通过get http请求下载文件
     * @param url 下载地址
     * @param param 参数
     * @return HttpServletResponse
     */
    public static HttpServletResponse PostHttpDownload(String url, String fileName, Map<String, String> param, ParamType paramType, HttpServletResponse response) throws Exception{
        return downloadFile.doPostHttpDownload(url, fileName, param, paramType, response);
    }


    /**
     * 通过get http请求下载文件
     * @param url 下载地址
     * @param param 参数
     * @return HttpServletResponse
     */
    public static HttpServletResponse doGetHttpDownload(String url, String fileName, Param param, HttpServletResponse response) throws Exception{
        return downloadFile.doGetHttpDownload(url, fileName, param, response);
    }

    /**
     * 通过get http请求下载文件
     * @param url 下载地址
     * @param param 参数
     * @return HttpServletResponse
     */
    public static HttpServletResponse doPostHttpDownload(String url, String fileName, Param param,ParamType paramType, HttpServletResponse response) throws Exception{
        return downloadFile.doPostHttpDownload(url, fileName, param, paramType, response);
    }

}
